<template>
  <div id="main-container" class="main-container" :class="$store.state.app.collapse?'position-collapse-left':'position-left'">
    <!-- 标签页 -->
    <div class="tab-container">
      <el-tabs class="tabs" :class="$store.state.app.collapse?'position-collapse-left':'position-left'"
        v-model="mainTabsActiveName" :closable="true" type="card"
        @tab-click="selectedTabHandle" @tab-remove="removeTabHandle">
        <el-dropdown class="tabs-tools" :show-timeout="0" trigger="hover">
          <div style="font-size:20px;width:50px;"><i class="el-icon-arrow-down"></i></div>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="tabsCloseCurrentHandle">关闭当前标签</el-dropdown-item>
            <el-dropdown-item @click.native="tabsCloseOtherHandle">关闭其它标签</el-dropdown-item>
            <el-dropdown-item @click.native="tabsCloseAllHandle">关闭全部标签</el-dropdown-item>
            <el-dropdown-item @click.native="tabsRefreshCurrentHandle">刷新当前标签</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <el-tab-pane v-for="item in mainTabs"
          :key="item.path" :label="item.title" :name="item.name">
          <span slot="label"><i :class="item.icon"></i> {{item.title}} </span>
        </el-tab-pane>
      </el-tabs>
    </div>
    <!-- 主内容区域 -->
    <div class="main-content">
      <keep-alive>
        <transition name="fade" mode="out-in">
            <router-view></router-view>
        </transition>
      </keep-alive>
    </div>
  </div>
</template>

<script>
import { getBreadName } from "@/utils/index";
import { mapState } from "vuex";
export default {
  inject:['reload'],
  data () {
    return {
    }
  },
  computed: {
    ...mapState({
      navTree: state => state.menu.navTree
    }),
    mainTabs: {
      get () { return this.$store.state.tab.mainTabs },
      set (val) { this.$store.commit('updateMainTabs', val) }
    },
    mainTabsActiveName: {
      get () { return this.$store.state.tab.mainTabsActiveName },
      set (val) { this.$store.commit('updateMainTabsActiveName', val) }
    }
  },
  methods: {
    // tabs, 选中tab
    selectedTabHandle (tab) {
      tab = this.mainTabs.filter(item => item.name === tab.name)
      if (tab.length >= 1) {
        this.$router.push({ path: tab[0].path ,params:tab[0].params,query: tab[0].query})
      }
      let path = tab[0].path;
      if(path == "/"){
        this.$store.commit("setIndexCurrent", true);
        this.$store.commit("setBreadName",JSON.stringify({ firstName: '首页' }));
        this.setMenu();
      }else{
        this.$store.commit("setIndexCurrent", false);
        getBreadName(path);
      }
    },
    // tabs, 删除tab
    removeTabHandle (tabName) {
      this.mainTabs = this.mainTabs.filter(item => item.name !== tabName)
      if (this.mainTabs.length >= 1) {
        // 当前选中tab被删除
        if (tabName === this.mainTabsActiveName) {
          this.$router.push({ path: this.mainTabs[this.mainTabs.length - 1].path }, () => {
            this.mainTabsActiveName = this.$route.name
          })
          this.screenTab();
        }
      } else {
        this.$router.push("/");
        this.$store.commit("setIndexCurrent", true);
        this.$store.commit("setBreadName",JSON.stringify({ firstName: '首页' }));
      }
    },
    // tabs, 关闭当前
    tabsCloseCurrentHandle () {
      this.removeTabHandle(this.mainTabsActiveName)
    },
    // tabs, 关闭其它
    tabsCloseOtherHandle () {
      this.mainTabs = this.mainTabs.filter(item => item.name === this.mainTabsActiveName)
    },
    // tabs, 关闭全部
    tabsCloseAllHandle () {
      this.mainTabs = []
      this.$router.push("/")
    },
    // tabs, 刷新当前
    tabsRefreshCurrentHandle () {
      this.reload();
    },
    //面包屑
    screenTab(){
      let param = this.$store.state.tab;
      let url = "";
      if (param && param.mainTabs.length>0) {
        param.mainTabs.forEach(f => {
          if (param.mainTabsActiveName == f.name) {
            url = f.path;
          }
        });
      }
      if(url == "/"){
        this.$store.commit("setIndexCurrent", true);
        this.$store.commit("setBreadName",JSON.stringify({ firstName: '首页' }));
        this.setMenu()
      }else{
        this.$store.commit("setIndexCurrent", false);
        getBreadName(url);
      }
    },
    //修改class选中的current值
    setMenu(){
      this.navTree.forEach(f=>{
        f.children.forEach(ele=>{
          ele.children.forEach(item=>{
            item.current=false;
          })
        })
      })
      return this.navTree
    }
  }
}
</script>

<style scoped lang="scss">
.main-container {
  background: #fff;
  .tabs {
    width: calc(100% - 50px);
    height: 40px;
    line-height: 40px;
    font-size: 14px;
    background: #fff;
    position: relative;
  }
  .tabs-tools {
    height: 40px;
    position: fixed;
    top: 50px;
    right: 0;
    cursor: pointer;
    border-left: 1px solid rgba(200, 206, 206, 0.5);
    border-right: 1px solid rgba(200, 206, 206, 0.5);
  }
  .tabs-tools:hover {
    background: rgba(200, 206, 206, 1);
  }
}
.position-collapse-left {
  left: 65px;
}

/deep/ .el-tabs__item.is-active{
  color: #25a4e4;
}
/deep/ .el-table th,.el-table th>.cell{  
    background: #405089 !important;
    color: #fff;
}
/deep/ .el-button--primary{
  background: #25a4e4;
  border-color:#25a4e4 ;
}
</style>
